// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

package com.netease.yunxin.kit.chatkit.ui.view.message.viewholder;

import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import androidx.annotation.NonNull;

import com.netease.nimlib.sdk.msg.attachment.NetCallAttachment;
import com.netease.nimlib.sdk.msg.constant.MsgDirectionEnum;
import com.netease.nimlib.sdk.msg.constant.MsgStatusEnum;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;
import com.netease.yunxin.kit.chatkit.model.IMMessageInfo;
import com.netease.yunxin.kit.chatkit.ui.common.MessageUtil;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatBaseMessageViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.databinding.ChatMessageTextViewHolderBinding;
import com.netease.yunxin.kit.chatkit.ui.model.ChatMessageBean;
import com.netease.yunxin.kit.chatkit.ui.view.message.MessageProperties;
import com.netease.yunxin.kit.corekit.im.IMKitClient;
import com.netease.yunxin.nertc.nertcvideocall.utils.NrtcCallStatus;

/** view holder for Text message */
public class ChatTextMessageViewHolder extends ChatBaseMessageViewHolder {

  ChatMessageTextViewHolderBinding textBinding;

  public ChatTextMessageViewHolder(@NonNull ChatBaseMessageViewHolderBinding parent, int viewType) {
    super(parent, viewType);
  }

  @Override
  public void addContainer() {
    textBinding =
        ChatMessageTextViewHolderBinding.inflate(
            LayoutInflater.from(parent.getContext()), getContainer(), true);
  }

  @Override
  public void bindData(ChatMessageBean message, ChatMessageBean lastMessage) {
    super.bindData(message, lastMessage);
    if (properties.getMessageTextSize() != MessageProperties.INT_NULL) {
      textBinding.messageText.setTextSize(properties.getMessageTextSize());
    }
    if (properties.getMessageTextColor() != MessageProperties.INT_NULL) {
      textBinding.messageText.setTextColor(properties.getMessageTextColor());
    }
    if (message.getMessageData().getMessage().getMsgType() == MsgTypeEnum.nrtc_netcall){
      textBinding.messageText.setText(getTextString(message));
    } else {
      MessageUtil.identifyFaceExpression(
              textBinding.getRoot().getContext(),
              textBinding.messageText,
              message.getMessageData().getMessage().getContent(),
              ImageSpan.ALIGN_BOTTOM);
    }
  }

  private String getTextString(ChatMessageBean message) {
    NetCallAttachment attachment = (NetCallAttachment) message.getMessageData().getMessage().getAttachment();
    String textString = "";
    switch (attachment.getStatus()) {
      case NrtcCallStatus.NrtcCallStatusComplete: //成功接听
        if (attachment.getDurations() == null) {
          break;
        }

        for (NetCallAttachment.Duration duration : attachment.getDurations()) {
          if (TextUtils.equals(duration.getAccid(), IMKitClient.account())) {
            textString = "[通话时长 "+secToTime(duration.getDuration())+"]";
          }
        }

        break;
      case NrtcCallStatus.NrtcCallStatusCanceled:
        textString = "已取消";
        break;

      case NrtcCallStatus.NrtcCallStatusRejected: { // "被拨打方" 拒绝接听电话或者正忙
        textString = message.getMessageData().getMessage().getDirect() == MsgDirectionEnum.In ? "已拒绝" : "被拒绝";
        break;
      }
      case NrtcCallStatus.NrtcCallStatusTimeout: //未接听
        textString = "未接听";
        break;
      case NrtcCallStatus.NrtcCallStatusBusy: { //对方正忙
        textString = message.getMessageData().getMessage().getDirect() == MsgDirectionEnum.In ? "未接听" : "对方正忙";
        break;
      }

      default:
        break;
    }
    return textString;
  }

  public String secToTime(int time) {
    String timeStr = null;
    int hour = 0;
    int minute = 0;
    int second = 0;
    if (time <= 0)
      return "00:00";
    else {
      minute = time / 60;
      if (minute < 60) {
        second = time % 60;
        timeStr = unitFormat(minute) + ":" + unitFormat(second);
      } else {
        hour = minute / 60;
        if (hour > 99)
          return "99:59:59";
        minute = minute % 60;
        second = time - hour * 3600 - minute * 60;
        timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
      }
    }
    return timeStr;
  }

  public String unitFormat(int i) {
    String retStr = null;
    if (i >= 0 && i < 10)
      retStr = "0" + Integer.toString(i);
    else retStr = "" + i;
    return retStr;
  }
}
